package com.zzh.oauth.intergration.authenticator;

import com.zzh.oauth.intergration.IntegrationAuthenticationEntity;
import com.zzh.oauth.intergration.pojo.UserPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @Author: yugu
 * @CreateDate: 2018/9/27$ 上午8:31$
 * @Description: 普通认证器（用户名+密码）
 */
@Component
@Primary
public class UsernamePasswordAuthenticator extends AbstractPreparableIntegrationAuthenticator {

//    @Autowired
//    private UserMapper mapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserPojo authenticate(IntegrationAuthenticationEntity entity) {
        String name = entity.getAuthParameter("username");
        String pwd = entity.getAuthParameter("password");
        if(name == null || pwd == null){
            throw new OAuth2Exception("用户名或密码不能为空");
        }
        UserPojo userPojo = new UserPojo();
        userPojo.setMobile("12345");
        userPojo.setName("12345");
        userPojo.setPwd("12345");
        return userPojo;
    }

    @Override
    public boolean support(IntegrationAuthenticationEntity entity) {
        return StringUtils.isEmpty(entity.getAuthType());
    }
}
